import _js from "../../cocos-libs/_/_js";
import _localStorage from "../../cocos-libs/_/_localStorage";
import _log from "../../cocos-libs/_/_log";
import _userInfo from "../../cocos-libs/_/_userInfo";

const Local_Default_Data: _IUserInfo = {
    money: 10000,
    health: 100,
    face: 100,
    message: [],
}

class UserInfo extends _userInfo {

    init(data?: _IUserInfo): void {
        this._data = Local_Default_Data;
        if (!data) {
            data = _localStorage.getItem(this.userinfoKey, this.encrypt);
        }
        super.init(data!);
        !data && this.onLoad();

        _log.info("用户信息初始化完成", this._data);
    }

    /**金钱 */
    setMoney(value: number) {
        this.data.money += value;
    }
    /**容貌 */
    setFace(value: number) {
        this.data.face += value;
    }
    /**健康 */
    setHealth(value: number) {
        this.data.health += value;
    }
    /**添加消息 */
    addMsg(id: string, time: string, seed: string) {
        this.data.message.push({ id, time, seed });
    }
    /**删除消息 */
    removeMsg(seed: string) {
        let index = this.data.message.findIndex(value => value.seed === seed);
        index >= 0 && _js.fastRemoveAt(this.data.message, index);
    }

}

export const user = new UserInfo();
//@ts-ignore
window["user"] = user;